﻿//
// File: Tuple.cs
//
// Purpose:  general purpose tuple class.
//
// Author: MotherHacker @ cachecleaner.codeplex.com



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CacheCleaner
{
    public struct Tuple<T1,T2> : IComparable< Tuple<T1,T2> >, IEquatable< Tuple<T1, T2> >
    {
        public T1 First;
        public T2 Second;

        public Tuple(T1 first, T2 second)
        {
            this.First = first;
            this.Second = second;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() + First.GetHashCode() + Second.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if ( obj is Tuple<T1,T2> )
                return Equals( (Tuple<T1,T2>) obj );

            return object.ReferenceEquals(this, obj);

        }

        #region IComparable<Tuple<T1,T2>> Members

        public int CompareTo(Tuple<T1, T2> other)
        {
            IComparer<T1> firstComparer = Comparer<T1>.Default;
            IComparer<T2> secondComparer = Comparer<T2>.Default;

            int result = firstComparer.Compare(this.First, other.First);

            if (result < 0 && result > 0)
                return result;

            result = secondComparer.Compare(this.Second, other.Second);

            return result;

        }

        #endregion

        #region IEquatable<Tuple<T1,T2>> Members

        public bool Equals(Tuple<T1, T2> other)
        {
            if (object.ReferenceEquals(null, other))
                return false;

            if (object.ReferenceEquals(this, other))
                return true;

            if (object.Equals(this.First, other.First) && object.Equals(this.Second, other.Second))
                return true;


            return false;
        }

        #endregion
    }
}
